/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

package com.huawei.api;

import com.huawei.api.bean.ClientParameter;
import com.huawei.api.exception.BaseCustomerException;
import com.huawei.api.parser.CommandLineParser;
import com.huawei.api.task.AnalyzeTask;
import com.huawei.api.utils.StringConvertUtil;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 启动类
 *
 * @since 2023/10/28
 */
@Slf4j
public class Main {
    private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);

    public static void main(String[] args) {
        try {
            LOGGER.info("formatcheck start successfully");
            printArgs(args);
            // 参数校验
            CommandLineParser parser = new CommandLineParser();
            // 获取测试用参数，打包时需要注释掉该方法
            // args = getTestArgs();
            parser.parse(args);
            // 初始化配置文件
            ClientParameter clientParameter = parser.getClientParameter();
            String branch = clientParameter.getBranch();
            if (StringUtils.isEmpty(branch) || branch.equalsIgnoreCase("null")) {
                clientParameter.setBranch("master");
            }
            LOGGER.info("--------------ClientParameter : {}-------------------", clientParameter.toString());
            AnalyzeTask task = new AnalyzeTask(clientParameter);
            LOGGER.info("--------------source : {}-------------------", clientParameter.getSource());
            task.execute();
        } catch (BaseCustomerException e) {
            LOGGER.debug("formatcheck exists some error occur in parse args failed, message is {}", e.getMessage());
            System.exit(1);
        }
        LOGGER.info("formatcheck end successfully");
        System.exit(0);
    }

    public static void printArgs(String[] args) {
        String param = Arrays.asList(args).stream().collect(Collectors.joining(" "));
        LOGGER.info(StringConvertUtil.convert(param, "="));
    }

    private static String[] getTestArgs() {
        String[] args;
        // 长度需要和参数一致，否则会报错
        args = new String[12];
        // 1-md文件路径，写入本地txt文件，2-扫描的源码绝对路径，3-分支，4-规则@符号隔开，5-报表文件夹路径
        args[0] = "--param_file_path"; // json参数的文件路径，由uccp插件写入到json中，然后传过来
        args[1] = "C:/Users/developer/Desktop/data/project-gitee/formatcheck/output/list.txt";
        args[2] = "--source"; // 扫描的项目目录
        args[3] = "C:/Users/developer/Desktop/data/project-gitee/formatcheck/docs";
        args[4] = "--branch"; // 源码路径
        args[5] = "master";
        args[6] = "--checker"; // 需要扫描的规则，以和工具方约定用@隔开，all为全部扫描
        args[7] = "C:/Users/developer/Desktop/data/project-gitee/formatcheck/output/checker.txt";
        args[8] = "--output"; // 输出报告文件路径。由于linux文件权限问题，工具会把日志打印到报表目录下.有写入权限
        args[9] = "C:/Users/developer/Desktop/data/project-gitee/formatcheck/output/";
        args[10] = "--tool_path";
        args[11] = "C:\\Users\\developer\\Desktop\\data\\project-gitee\\formatcheck";

        return args;
    }
}
